package com.apes.scm.masterdata.frame.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.fileserver.File;
import com.apes.scm.masterdata.area.model.Area;
import com.apes.scm.masterdata.area.model.BusinessCircle;
import com.apes.scm.masterdata.area.model.Country;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.merchant.model.Merchant;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 功能：门店信息
 *
 * @author  LinWZ
 * @create 2019-11-07 16:00
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Store.CONST_TABLE_NAME, indexes = {@Index(columnList = "dept_id", unique = true)})
public class Store extends Model  {
    public static final String CONST_TABLE_NAME = "mas_store";

    @Id
    @Column(name = "id", length = 30)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "部门编码")
    @Comment("部门编码")
    private String id;  //部门编码

    @Name
    @Column(name = "code", length = 12)
    @Comment("门店编码")
    private String code;

    @Name
    @Column(name = "name", nullable = false, length = 120)
    @Comment("门店名称")
    private String name; //门店名称

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id",referencedColumnName = "id")
    @Comment("业务部门")
    private Dept dept;  //业务部门 (门店编码)

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("所属公司")
    private PartyRole company; //所属公司

    @OneToOne(fetch = FetchType.LAZY,optional = false)
    @JoinColumn(name = "branch_entry_id",referencedColumnName = "id")
    @Comment("开店申请")
    private BranchEntry branchEntry;  //开店申请

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "store_Type_Name", referencedColumnName = "id")
    @Comment("门店类型")
    private Enumeration storeType; //门店类型

    @Column(name = "fckbj", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("封库标记")
    private boolean fckbj=false;

    @Column(name = "zyzfn", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("直营转赋能")
    private boolean zyzfn=false;

    @Column(name = "sinopec", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("中石化门店")
    private boolean sinopec=false;

    @Column(name = "contain_space",  nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("空间属性")
    private boolean containSpace=false;

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "outsourcing_approval", referencedColumnName = "id")
    @Comment("外采审批")
    private Enumeration outsourcingApproval;

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "store_levels", referencedColumnName = "id")
    private Enumeration storeLevels; //门店等级

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note; //备注

//    @Name
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "Business_Circle", referencedColumnName = "id")
//    private Enumeration businessCircle; //商圈

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "business_circle_id", referencedColumnName = "id")
    @Comment("商圈")
    private BusinessCircle businessCircle;

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "merchant_id", referencedColumnName = "id")
    @Comment("商户")
    private Merchant merchant;

    @Column(name = "store_Space", nullable = false, length = 120)
    @Comment("门店面积")
    private Double storeSpace; //门店面积

    @Column(name = "store_space_note", length = 250)
    @Comment("店面面积备注")
    private String storeSpaceNote;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "country_id", referencedColumnName = "id")
    @Comment("国家")
    private Country country; //国家

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "province_id", referencedColumnName = "id")
    @Comment("省份")
    private Area province; //省份

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "city_id", referencedColumnName = "id")
    @Comment("城市")
    private Area city; //城市

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "county_id", referencedColumnName = "id")
    @Comment("区县")
    private Area county; //区县

    @Column(name = "address", length = 500)
    @Comment("地址")
    private String address; //地址

    @Column(name = "telephone", length = 500)
    @Comment("联系电话")
    private String telephone; //联系电话

    @Column(name = "boutique_room", length = 500)
    @Comment("精品间面积")
    private String boutiqueRoom; //精品间面积

    @Column(name = "Warehouse", length = 500)
    @Comment("仓库面积")
    private String Warehouse; //仓库面积

    @Column(name = "rental_Area", length = 500)
    @Comment("外租面积")
    private String rentalArea ; //外租面积

    @Column(name = "calculation_Rental", length = 500)
    @Comment("计租面积")
    private String calculationRental; //计租面积

    @Column(name = "receiving_day")
    @Comment("接场日期")
    private Date receivingDay;//接场日期


    @Column(name = "open_day")
    @Comment("开业时间")
    private Date openDay;//开业时间


    @Column(name = "banner_length", length = 20)
    @Comment("横幅长度")
    private String bannerLength; //横幅长度

    @Column(name = "LED_length", length = 20)
    @Comment("led屏长度")
    private String LEDLength; //led屏长度

    @Column(name = "decoration", nullable = false, length = 120)
    @Comment("装修投入")
    private Double decoration; //装修投入【元】

    @Column(name = "equipment", nullable = false, length = 120)
    @Comment("设备投入")
    private Double equipment ; // 设备投入【元】

    @OneToMany(mappedBy = "store", cascade=CascadeType.ALL, orphanRemoval = true)
    @Comment("服务工位")
    private List<Station> stations = new ArrayList<>(); //服务工位

    public List<Station> getStations() {
        for (Station station : stations) {
            station.setStore(this);
        }
        return stations;
    }
    public void setStations(List<Station> stations) {
        if (stations == null) stations = new ArrayList<>();
        this.stations.clear();
        this.stations.addAll(stations);
    }

    @Column(name = "longitude")
    @Comment("经度")
    private double longitude;

    @Column(name = "latitude")
    @Comment("纬度")
    private double latitude;


    @Column(name = "business_day")
    @Comment("营业日期起")
    private Date businessDay;

    @Column(name = "business_day_end")
    @Comment("营业日期止")
    private Date businessDayEnd;


    @Column(name = "open_preheat_day")
    @Comment("开业预热")
    private Date openPreheatDay;

    @Column(name = "closing_day")
    @Comment("闭店日期")
    private Date closingDay;

    @Column(name = "ORIGIN", length = 20)
    @Comment("外部ID")
    private String origin; //外部ID

    @Column(name = "car_repair_number", length = 255)
    @Comment("汽车维修账号")
    private String carRepairNumber;

    @Column(name = "car_repair_password", length = 255)
    @Comment("汽车维修账号密码")
    private String carRepairPassword;

//    @Name
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "car_repair_platform", referencedColumnName = "id")
//    private Enumeration carRepairPlatform; //汽车维修平台

    @Column(name = "up_car_repair", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("上传维修记录给第三方平台标记")
    private boolean upCarRepair =false;

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fn_tag", referencedColumnName = "id")
    @Comment("赋能标签")
    private Enumeration fnTag;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "LOGO",columnDefinition = "CLOB")
    @Comment("LOGO")
    private String logo;

    @OneToMany(mappedBy = "store", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<StoreAreaInquiry> storeAreaInquiries = new ArrayList<>();

    public List<StoreAreaInquiry> getStoreAreaInquiries() {
        if (storeAreaInquiries == null) return new ArrayList<>();
        for (StoreAreaInquiry item : storeAreaInquiries) {
            item.setStore(this);
        }
        return storeAreaInquiries;
    }

    @Transient
    private String citys;

    @Transient
    private com.alibaba.fastjson.JSONObject storeInfo;

    // 一个方块显示多个城市
    public String getCitys(){
        String citys = "";
        if (storeAreaInquiries.size() == 0)
            return citys;
        for (StoreAreaInquiry item : storeAreaInquiries) {
            if(item.isValid()) {
                citys += "[" + item.getCity().getId() + " "+ item.getCity().getName() + "]";
                citys += ";";
            }
        }
        return citys;
    }

}
